#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QApplication>
#include <QButtonGroup>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QFuture>
#include <QFutureWatcher>
#include <QLabel>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QThread>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>
#include <QtConcurrent/QtConcurrent>

#include "shadowwidget.h"
#include "typedef.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

// 样式定义
const QString STYLE_STEP_SELECTED(
    "QLabel{"
    "font-family: 'Microsoft YaHei';"
    "border: 2px solid #ffa043;"
    "font-size: 16px;"
    "background-color: #ffffff;"
    "color: #79828d;"
    "}");

const QString STYLE_STEP_UNSELECTED(
    "QLabel{"
    "font-family: 'Microsoft YaHei';"
    "border: 2px solid #bdc3d1;"
    "font-size: 16px;"
    "background-color: #ffffff;"
    "color: #768aa3;"
    "}");

const QString STYLE_STEP_DONE_WITH_CHECK_MARK(
    "QLabel{"
    "font-family: 'Microsoft YaHei';"
    "border: 2px solid #d8e6f6;"
    "font-size: 16px;"
    "background-image: "
    "url(:/resource/01.png);"
    "background-position: center left;"  // 水平位置在左边
    "background-repeat: no-repeat;"
    "background-color: #ffe7d0;"
    "color: #ffa043;"
    "}");

class StyleSwitcher : public QObject {
  Q_OBJECT

 public:
  StyleSwitcher(QLabel *label) : m_label(label), m_timer(nullptr) {
    // 初始化定时器
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &StyleSwitcher::switchStyle);
    m_timer->start(1000);
  }
  ~StyleSwitcher() {
    DELETE_PTR(m_label);
    DELETE_PTR(m_timer);
  }
 public slots:
  void switchStyle() {
    static int currentState = 0;

    // 根据当前状态切换样式
    switch (currentState) {
      case 0:
        m_label->setStyleSheet(STYLE_STEP_UNSELECTED);
        currentState = 1;
        break;
      case 1:
        m_label->setStyleSheet(STYLE_STEP_SELECTED);
        currentState = 2;
        break;
      case 2:
        m_label->setStyleSheet(STYLE_STEP_DONE_WITH_CHECK_MARK);
        currentState = 0;
        break;
    }
  }

 private:
  QLabel *m_label;
  QTimer *m_timer;
};

class MainWindow : public QMainWindow {
  Q_OBJECT

 public:
  MainWindow(QWidget *parent = nullptr);
  ~MainWindow();

 private:
  void initGrpBox(void);
  void initBtnGrp(void);
  void initTogglePart(void);
  void initFileOperation(void);
  void limitInput(void);
  void showMaskLayer(void);
  void openSingleFile(void);
  void openMultipleFiles(void);
  void openSrcDir(void);
  void openTargetDir(void);
  void fileOperation(File_Oper buttonId);
  bool operateAFile(File_Oper buttonId, QString &srcPath, QString &targetPath);
  bool operateDir(File_Oper buttonId, QString &srcDir, QString &targetDir);
  bool operateFiles(File_Oper buttonId, QStringList &srcFiles,
                    QString &targetDir);
  void showFileOperationMask(void);

 private slots:
  void slotComboBoxClicked(void);
  void slotBtnGrpClicked(QAbstractButton *);
  void slotBtnFileOperClicked(QAbstractButton *);

 private:
  Ui::MainWindow *ui;
  StyleSwitcher *m_switcher;
  QMenu *m_toggleMaskMenu;
  ShadowWidget *m_shadowWidget;
  struct BtnGrpInfo {
    QPushButton *checkedBtn = nullptr;
    QString strText = "";
  } m_prevCheckedBtnInfo;
  QString m_srcDir;
  QString m_targetDir;
  QStringList m_srcFiles;
};
#endif  // MAINWINDOW_H
